<?php
    require_once 'public/includes/functions.php';
    require_once 'models/container.php';
    require_once 'models/street.php';
    
    class MapController extends functions {
        
        var $container =null;
        var $street = null; //doi tuong cua street table
        var $autoComplete = array(); //lay thong tin trong csdl de load tu dong khi go dia diem
        var $infoAllMark =null;
        var $infoMark;
        var $from ;// == diem dau defaul (to direction)
        var $to = null; // == diem cuoi defaul (to direction)
        var $fromDirection=null; //== diem đầu khi tìm đường (to direction)
        var $toDirection=null; // == diểm cuối khi tìm đường (to direction)
        
        function __construct(){
            
            //dung cho mainlef
            $this->container = new Container_Model();   
            $this->street = new Street_Model();   
            $this->getDataToAutoComplete();      
        }

        public function indexAtion(){
            
        }
        
        /**
         * lay du lieu trong csdl de load vao cac combobox tim dia diem mot cach tu dong khi
         * go vao (giong google)
         * 
         * */
        private function getDataToAutoComplete(){
            $res = $this->street->ExecuteSQL("SELECT * FROM street INNER JOIN location ON street.st_id = location.st_id");
            while($row = mysql_fetch_assoc($res)){
                $this->autoComplete[] =array(
                                    'name'      => $row['L_Name'],
                                    'address'   => $row['SoDuong']." - ".$row['TenDuong']." - ".$row['L_Name'],                                    
                                    'lat'       => $row['Latitude'],
                                    'lng'       => $row['Longitude']
                                );
            }
        }
        
                  
        public function getInfoData($where=null){
            //VE DUONG KHI SUBMIT FORM
            if(isset($_POST['cmdXd_duong'])){
                if(empty($_POST['fromStreet']) || empty($_POST['txtto1'])){
                    return;
                }
                $addressFrom = explode("-",$_POST['fromStreet']);
                $addressTo   = explode("-",$_POST['txtto1']);
                
                if(!empty($addressFrom[0]) && $addressFrom[0]!=" "){
                    $sqlFrom = "SELECT * FROM street 
                                            INNER JOIN location 
                                            ON street.st_id = location.st_id 
                                            WHERE SoDuong = ".trim($addressFrom[0])." 
                                                and TenDuong ='".trim($addressFrom[1])."'";  
                                                           
                }else{
                    $sqlFrom = "SELECT * FROM street 
                                            INNER JOIN location 
                                            ON street.st_id = location.st_id 
                                            WHERE TenDuong ='".trim($addressFrom[1])."' and
                                                  L_Name ='".trim($addressFrom[2])."'";
                }
                
                if(!empty($addressTo[0]) && $addressTo[0] != " "){
                    $sqlTo = "SELECT * FROM street 
                                            INNER JOIN location 
                                            ON street.st_id = location.st_id 
                                            WHERE SoDuong =".trim($addressTo[0]) ." 
                                                and TenDuong ='".trim($addressTo[1])."'";
                }else{
                    $sqlTo = "SELECT * FROM street 
                                            INNER JOIN location 
                                            ON street.st_id = location.st_id 
                                            WHERE L_Name ='".trim($addressTo[2])."' and
                                                  TenDuong ='".trim($addressTo[1])."'";
                }
                
              
                $resFrom    = $this->street->ExecuteSQL($sqlFrom);
                while($row = mysql_fetch_assoc($resFrom)){
                   
                    $this->from[] = array(
                            'lat'       => $row['Latitude'],
                            'lng'       => $row['Longitude'],
                            'name'      => $row['L_Name'],
                            'address'   => $row['SoDuong']." - ".$row['TenDuong']                            
                        );
                }
                
                $resTo   =  $this->street->ExecuteSQL($sqlTo);
                while($row = mysql_fetch_assoc($resTo)){
                    $this->to[] = array(
                        'lat'       => $row['Latitude'],
                        'lng'       => $row['Longitude'],
                        'name'      => $row['L_Name'],
                        'address'   => $row['SoDuong']." - ".$row['TenDuong']
                    );
                }
           //=== END SUBMIT FORM           
           }else{ //HIEN THI MAC DINH
                if($where!=null){
                    $res = $this->street->ExecuteSQL(
                                        "SELECT * FROM street 
                                                INNER JOIN location 
                                                ON street.st_id = location.st_id 
                                                WHERE ".$where
                            );
                            
                }else{
                    $res = $this->street->ExecuteSQL("SELECT * FROM street 
                                                        INNER JOIN location 
                                                        ON street.st_id = location.st_id "
                                        );
                }
                //
                //LAY THONG TIN DE HIEN THI MAC DINH       
                while($row = mysql_fetch_assoc($res)){
                    $this->infoData[] = array(
                                            'name'      => $row['L_Name'],
                                            'address'   => $row['TenDuong'],
                                            'soDuong'   => $row['SoDuong'],
                                            'lat'       => $row['Latitude'],
                                            'lng'       => $row['Longitude']  
                            );                               
                }
            }// === END IF-ELSE                               
        }
        
        //
        // == VE DUONG DI TU DIEM HIEN TAI TOI DIEM CAN TOI
        public function getDirectionTipForm(){
            if(isset($_POST['cmdTipForm'])){
                //
                // == toa do diem ban dau
                $this->fromDirection = array(
                            'lat'       => $_POST['lat'],
                            'lng'       => $_POST['lng']                                                        
                        );
                
                $addressTo   = explode("-",$_POST['toDuong']);
                
                // == toa do diem den
                if(!empty($addressTo[0]) && $addressTo[0] != " "){
                    $sqlTo = "SELECT * FROM street 
                                            INNER JOIN location 
                                            ON street.st_id = location.st_id 
                                            WHERE SoDuong =".trim($addressTo[0]) ." 
                                                and TenDuong ='".trim($addressTo[1])."'";
                }else{
                    $sqlTo = "SELECT * FROM street 
                                            INNER JOIN location 
                                            ON street.st_id = location.st_id 
                                            WHERE TenDuong ='".trim($addressTo[1])."'";
                }
                
                $resTo   =  $this->street->ExecuteSQL($sqlTo);
                $row = mysql_fetch_assoc($resTo);
                $this->toDirection = array(
                        'lat'       => $row['Latitude'],
                        'lng'       => $row['Longitude'],                    
                    );
            }
                  
            
        }// == END FUNFION getDirectionTipForm()
        
        /**
         * Load one marker info 
         * @param $id: ma bai viet de xac dinh lng and lat
         * @return  
         * */
        public function loadInfoOneMarker($id){
            $row = $this->street->getAll('container','Id = '.$id);          
                        
            if(empty($row['kinhdo']) || empty($row['vido'])){                                
                  $infoMark ="<center>
                                <font color='red'>Bản đồ chưa được cập nhật, vui lòng click 
                                    <a href='./?rt=page&id=".$_GET['id']."><b>Click</b></a> để quay lại
                                </font>
                              </center>";
                              
            }else{ // == if ton tai marker
                $this->infoMark = $row;                               
            }               
        }
    }
    
    // thuc thi class
    $map = new MapController();
    //
    //load thong tin cua mot dia diem khi click tu bai viet
    if(isset($_GET['rt']) && ($_GET['rt'])=='map'){
        if(isset($_POST['cmdXd_duong'])){
            $map->getInfoData();
                
        }else{
            
            $id = isset($_GET['id']) ?  $_GET['id'] : null;
            if($id !=null) {            
                $map->loadInfoOneMarker($id);
                
                //
                // === Where : - diem du lich or nhahang or khachsan ...
            }else if(isset($_GET['wh']) && !empty($_GET['wh'])){
                $where =null;
                switch($_GET['wh']){
                    case 'alldulich': 
                        $where = "location.catId = 'khudulich' or 
                                    location.catId = 'danhlam' or 
                                    location.catId = 'ditich'";
                        break;
                    default : 
                        $where = "L_id=".$_GET['wh'];
                        break;
                }            
                $map->getInfoData($where);
                
            }else{
                $map->getInfoData();
            }    
        }
        
        //
        // == DIRECTION TIP FORM
        if(isset($_POST['cmdTipForm'])){
            $map->getDirectionTipForm();    
        }
    }
    $map->show('map');
?>       
